package com.jacky.zkclient.base;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.ZkConnection;

/**
 * 简单通过zkClient连接zk进行节点CRUD
 */
public class ZkClientBase {

	/** zookeeper地址 */
	static final String CONNECT_ADDR = "192.168.6.131:2181";
	/** session超时时间 */
	static final int SESSION_OUTTIME = 5000;//ms
	
	
	public static void main(String[] args) throws Exception {

		ZkClient zkc = new ZkClient(CONNECT_ADDR, 5000000,SESSION_OUTTIME);
		zkc.createPersistent("/super");
		zkc.createEphemeral("/super/temp","789");
		while (true){
			Thread.sleep(200000);
		}

		//创建临时节点

		//1. create and delete方法 
	/*	zkc.createEphemeral("/temp");
		zkc.createPersistent("/super/c1", true);
		Thread.sleep(10000);
		zkc.delete("/temp");
		zkc.deleteRecursive("/super");*/
		
		//2. 设置path和data 并且读取子节点和每个节点的内容
	/*	zkc.createPersistent("/super", "1234");
		zkc.createPersistent("/super/c1", "this is c1 content");
		zkc.createPersistent("/super/c2", "this is c2 content");
		//获得super下面的子节点
		List<String> list = zkc.getChildren("/super");
		for(String p : list){
			System.out.println(p);
			String rp = "/super/" + p;
			String data = zkc.readData(rp);
			System.out.println("节点为：" + rp + "，内容为: " + data);
		}*/
		
		//3. 更新和判断节点是否存在
		/*zkc.writeData("/super/c1", "新内容");
		Object data = zkc.readData("/super/c1");
		System.out.println(data);
		System.out.println(zkc.exists("/super/c1"));
		//4.递归删除/super内容
		zkc.deleteRecursive("/super");*/
	}
}
